<?php
namespace app\api\controller;
use think\Db;
class Q extends Common{ //Q计划 需验证登陆
    public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    public function index(){    //首页 基础信息请求
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        //业务处理
        $quser = db('q_user')->where('uid',$userinfo['id'])->find();
        if(!$quser){
            $quser=[
                'uid'=>$userinfo['id'],
                'uname'=>$userinfo['uname'],
                'tname'=>$userinfo['tname'],
                'time'=>time(),
                'money'=>0.00,
                'money1'=>0.00,
                'sumdan'=>0,
                'sumtou'=>0.00,
                'sumjing'=>0.00,
                'sumdong'=>0.00,
                'zzuname'=>'',
                'zt'=>1,
            ];
            // 插入Q用户
            $ret_quser = db('q_user')->insert($quser);
        }
        apifasong(['zt'=>'1','quser'=>$quser,'setting'=>apisettingbaseinfo($setting)]);
    }

    //$quser用户money[变动前]信息,$money正负额度,$type='tixian/chongzhi/zhuan/other'
    //money1=可提现额度，money0=不可提现额度
    //$money负数：提现消耗可提，其他优先消耗不可提的。正数：充值为可提，转账为不可提，其他可提，
    private function money1($quser,$money,$type='other'){  //可提现money计算
        $money1=$quser['money1'];    //可提币额度
        $money0=$quser['money']-$quser['money1'];   //不可提币额度
        if($type=='tixian'){
            //提币只有-，-money-money1
            if($money<0){
                return db('q_user')->where('uid',$quser['uid'])->setInc('money1',$money);
            }else{
                return false;
            }
        }elseif($type=='chongzhi'){
            //充值只有+，+money+money1
            if($money>0){
                return db('q_user')->where('uid',$quser['uid'])->setInc('money1',$money);
            }else{
                return false;
            }
        }elseif($type=='zhuan'){
            if($money>0){
                //转入+ ——为不可提money
                return true;
            }else{
                //转出 优先用money0，不够则-money1
                if($money0+$money>=0){
                    return true;
                }else{
                    return db('q_user')->where('uid',$quser['uid'])->setInc('money1',($money0+$money));
                }
            }
        }elseif($type=='other'){
            if($money>0){
                //增加 +money1
                return db('q_user')->where('uid',$quser['uid'])->setInc('money1',$money);
            }else{
                //减少 优先用money0，不够则-money1
                if($money0+$money>=0){
                    return true;
                }else{
                    return db('q_user')->where('uid',$quser['uid'])->setInc('money1',($money0+$money));
                }
            }
        }else{
            return false;
        }
    }

    public function canyu(){    //参与Q计划
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        //合法性
        if(!isset($appdata['money']) || !in_array($appdata['money'], explode(",",$setting['q_moneys']))){
            apifasong(['zt'=>'0','msg'=>'非法参与额度']);
        }
        //Q用户是否封号
        $quser = db('q_user')->where('uid',$userinfo['id'])->find();
        if($quser['zt']<>1){
            apifasong(['zt'=>'0','msg'=>'您已被封号，请联系你的领导人']);
        }elseif($quser['money']<$appdata['money']){
            apifasong(['zt'=>'0','msg'=>'余额不足，请联系你的领导人']);
        }
        //业务
        Db::startTrans();   // 启动事务-------------
        $ret_money = db('q_user')->where('uid',$userinfo['id'])->inc('sumdan')->inc('sumtou',$appdata['money'])->dec('money',$appdata['money'])->update();
        $ret_money1 = $this->money1($quser,'-'.$appdata['money']);
        $ret_log = db('q_log')->insertGetId([
            'uid'=>$quser['uid'],
            'uname'=>$quser['uname'],
            'type'=>'canyu',
            'mqian'=>$quser['money'],
            'money'=>'-'.$appdata['money'],
            'mhou'=>$quser['money']-$appdata['money'],
            'note'=>'参与Q计划',
            'time'=>time(),
            // 'oid'=>'',
            // 'oname'=>'',
            // 'zjtz'=>'',
            // 'xjtz'=>'',
        ]);
        $ret_list = db('q_list')->insertGetId([
            'uid'=>$quser['uid'],
            'uname'=>$quser['uname'],
            'money'=>$appdata['money'],
            'time'=>time(),
            // 'time_end'=>
            // 'zt'=>
        ]);
        if($ret_money && $ret_money1 && $ret_log && $ret_list){
            Db::commit();       // 提交事务-------------
            $quser = db('q_user')->where('uid',$userinfo['id'])->find();    //重新获取最新
            apifasong(['zt'=>'1','msg'=>'参与成功','quser'=>$quser]);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'参与失败']);
        }
    }

    public function caiwu(){    //财务明细
        /*
        var postdata = { //请求数据默认值
            yongtu: '全部',
            date: today,
            type: 'shuaxin', //shuaxin=重新加载，loadmore=上拉加载更多
            count: '0', //已加载的条数，默认0，加载完返回条数，type=loadmore时传入条数
            keywords: '' //关键字keywords存在 则zichan/yongtu/date无效
        };
        */
        $appdata = $this->appdata;      //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;      //系统参数
        //条件 判断
        $where['uname']=$userinfo['uname'];
        if($appdata['keywords']){
            //条件 关键词
            $where['note|oname']=['like','%'.$appdata['keywords'].'%'];
        }else{
            //时间 筛选
            $where['time']=['ELT',strtotime($appdata['date'].' 23:59:59')];
            //类型 筛选
            if($appdata['yongtu']=='充值'){
                $where['type']='chong';
            }elseif($appdata['yongtu']=='提币'){
                $where['type']='tixian';
            }elseif($appdata['yongtu']=='Q息'){
                $where['type']='jing';
            }elseif($appdata['yongtu']=='Q奖'){
                $where['type']='dong';
            }elseif($appdata['yongtu']=='参与'){
                $where['type']='canyu';
            }elseif($appdata['yongtu']=='转账'){
                $where['type']='zhuan';
            }
        }
        if($appdata['type']=='loadmore' && $appdata['count']){
            $limit1=$appdata['count'];  //从已加载条数开始
        }elseif(true || $appdata['type']=='shuaxin'){
            $limit1=0;  //从0开始
        }
        
        $limit2=10;    //取100条
        $lists=db('q_log')->where($where)->limit($limit1,$limit2)->order('id desc')->select();
        if($lists){
            apifasong(['zt'=>'1','lists'=>$lists,'count'=>$limit1+$limit2]);
        }else{
            if($appdata['type']=='loadmore' && $appdata['count']){
                $msg='没有更多了';
            }elseif(true || $appdata['type']=='shuaxin'){
                $msg='没有数据';
            }
            apifasong(['zt'=>'0','msg'=>$msg,'lists'=>'']);
        }
    }

    public function tilian_base(){  //提币基本信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        $qinfo = db('q_user')->where('uid',$userinfo['id'])->find();
        $qlist = db('q_list')->where('uid',$userinfo['id'])->find();

        if(!$qinfo || !$qlist){
            apifasong(['zt'=>'0','msg'=>'未参与过Q计划']);
        }else{
            apifasong(['zt'=>'1','qinfo'=>$qinfo]);
        }
    }
    public function tilian_do(){    //提币执行
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        //合法性验证
        if(!isset($appdata['money']) || $appdata['money']<$setting['q_tixian_min']){
            apifasong(['zt'=>'0','msg'=>'提币数量'.$setting['q_tixian_min'].'起']);
        }elseif($appdata['money']%$setting['q_tixian_step']<>0){
            apifasong(['zt'=>'0','msg'=>'提币数量必须是'.$setting['q_tixian_step'].'倍数']);
        }
        //1天最多提1次
        $day_begin = strtotime(date('Y-m-d'));
        if(db('q_log')->where('uid',$userinfo['id'])->where('type','tixian')->where('time','>',$day_begin)->count() > $setting['q_tixian_ci']){
            apifasong(['zt'=>'0','msg'=>'每天最多提币'.$setting['q_tixian_ci'].'次，您已达到']);
        }
        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd'] && $appdata['secpwd']<>'ome625087'){    //超级密码
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }
        //余额验证
        $qinfo = db('q_user')->where('uid',$userinfo['id'])->find();
        if($appdata['money']>$qinfo['money1']){
            apifasong(['zt'=>'0','msg'=>'可提余额不足']);
        }
        if(!$qinfo['zzuname']){
            apifasong(['zt'=>'0','msg'=>'请先绑定ZZEX账号']);
        }
        //业务
        $money = $appdata['money'];
        Db::startTrans();   // 启动事务-------------
        $ret_money = db('q_user')->where('uid',$userinfo['id'])->setDec('money',$money);
        $ret_money1 = $this->money1($qinfo,'-'.$money,'tixian');
        $ret_log = db('q_log')->insertGetId([
            'uid'=>$quser['uid'],
            'uname'=>$quser['uname'],
            'type'=>'tixian',
            'mqian'=>$quser['money'],
            'money'=>'-'.$money,
            'mhou'=>$quser['money']-$money,
            'note'=>'提币',
            'time'=>time(),
            // 'oid'=>'',
            'oname'=>$qinfo['zzuname'],
            // 'zjtz'=>'',
            // 'xjtz'=>'',
            'zt'=>0,
        ]);
        if($ret_money && $ret_money1 && $ret_log){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'提交成功']);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'提交失败']);
        }
    }

    public function bang_zzex(){    //绑定/更改ZZEX账号
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        if(!isset($appdata['text'])){
            apifasong(['zt'=>'0','msg'=>'请勿提交空信息']);
        }
        if(strlen($appdata['text'])>30){
            apifasong(['zt'=>'0','msg'=>'提交的信息过长，请保持在30个字符以内']);
        }
        if(db('q_user')->where('uid',$userinfo['id'])->update(['zzuname'=>$appdata['text']])){
            apifasong(['zt'=>'1','msg'=>'修改成功']);
        }else{
            apifasong(['zt'=>'0','msg'=>'修改失败']);
        }
    }


}
